package com.example.hxds.mps.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.mps.service.MapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

/**
 * @Author XUYANG
 * @Date 2024-03-16 13:48
 */
@Service
@Slf4j
public class MapServiceImpl implements MapService {
    private String distanceUrl="https://apis.map.qq.com/ws/distance/v1/matrix/";
    private String directionUrl="https://apis.map.qq.com/ws/direction/v1/driving";
    @Value("${tencent.map.key}")
    private String key;
    @Override
    public HashMap estimateOrderMileageAndMinute(String mode, String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {
        HttpRequest req=new HttpRequest(distanceUrl);
        req.form("mode",mode);
        req.form("from",startPlaceLatitude+","+startPlaceLongitude);
        req.form("to",endPlaceLatitude+","+endPlaceLongitude);
        req.form("key",key);
        HttpResponse response= req.execute();
        JSONObject json= JSONUtil.parseObj(response.body());
        int status=json.getInt("status");
        String message=json.getStr("message");
        System.out.println(message);
        if (status!=0){
            log.error(message);
            throw new HxdsException("预估里程异常"+message);
        }
        JSONArray rows=json.getJSONObject("result").getJSONArray("rows");
        JSONObject element=rows.get(0,JSONObject.class).getJSONArray("elements").get(0,JSONObject.class);
        int distance=element.getInt("distance");
        String mileage=new BigDecimal(distance).divide(new BigDecimal(1000)).toString();
        int duration=element.getInt("duration");
        String tmp=new BigDecimal(duration).divide(new BigDecimal(60),0, RoundingMode.CEILING).toString();
        int minute=Integer.parseInt(tmp);
        HashMap res=new HashMap<>(){{
            put("mileage",mileage);
            put("minute",minute);
        }};
        return res;
    }

    @Override
    public HashMap calculateDriverLine(String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {
        HttpRequest request=new HttpRequest(directionUrl);
        request.form("from",startPlaceLatitude+","+startPlaceLongitude);
        request.form("to",endPlaceLatitude+","+endPlaceLongitude);
        request.form("key",key);
        HttpResponse response=request.execute();
        JSONObject json=JSONUtil.parseObj(response.body());
        int status=json.getInt("status");
        if (status!=0){
            throw new HxdsException("执行异常");
        }
        JSONObject res=json.getJSONObject("result");
        HashMap map=res.toBean(HashMap.class);
        return map;
    }
}
